package com.yiren.manager.ctrl.beds;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.StringUtil;
import com.yiren.core.exception.MyException;
import com.yiren.core.exception.SwallowException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.JsonMapperTool;
import com.yiren.core.utils.UUIDGenerator;
import com.yiren.entity.YrFloor;
import com.yiren.entity.YrSubjectRoom;
import com.yiren.manager.ctrl.GenericController;
import com.yiren.manager.form.beds.YrBedForm;
import com.yiren.manager.form.beds.YrFloorForm;
import com.yiren.manager.service.iface.beds.YrFloorService;
import com.yiren.manager.service.iface.department.YrSubjectRoomService;
import com.yiren.manager.utils.ObjectUtils;

@Controller
@RequestMapping("floor")
public class YrFloorController extends GenericController<YrFloorForm, YrFloor>{
	
	@Autowired
	private YrFloorService yrFloorService;
	
	@Autowired
	private YrSubjectRoomService subjectRoomService;
	
	//保存实体
	@RequestMapping("/save")
	@ResponseBody
	public HTTPResult<YrBedForm> save(String data) throws SwallowException, MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IntrospectionException{
		HTTPResult<YrBedForm> result = new HTTPResult<YrBedForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		YrFloorForm form = tool.mapper("floor", YrFloorForm.class);
		if(null==form){
			throwExp("param.error");
		}
		if(null==form.getFloorId()||form.getFloorId().isEmpty()){
			form.setFloorId(UUIDGenerator.getUUID());
			form.setCreateTime(new Date());
		}
		form.setUpdateTime(new Date());
		YrFloor entity = ObjectUtils.objectToObject(form, YrFloor.class);
		yrFloorService.save(entity);
		
		return result;
	}
	
	
	/**获取所有记录*/
	@RequestMapping("findAll")
	@ResponseBody
	public HTTPResult<YrFloorForm> findAll() throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("floorName", "ASC");
		QueryResult<YrFloor> qr = yrFloorService.find(YrFloor.class, null, new Object[]{},map);
		List<YrFloor> entityList = qr.getResultList();
		List<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		for(YrFloor building : entityList){
			YrFloorForm form = ObjectUtils.objectToObject(building, YrFloorForm.class);
			formList.add(form);
		}
		result.setData(formList);
		return result;
	}
	
	/**
	 * 根据楼房id获取楼层列表
	 * @param buildingId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("find")
	@ResponseBody
	public HTTPResult<YrFloorForm> find(String buildingId) throws Exception{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		if(null==buildingId){
			throwExp("param.error");
		}
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("floorName", "ASC");
		
		String sqlWhere = "building_id=?1";
		QueryResult<YrFloor> qr =  yrFloorService.find(YrFloor.class, sqlWhere, new Object[]{buildingId},map);
		
		List<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		for(YrFloor floor : qr.getResultList()){
			formList.add(toForm(floor));
		}
		result.setData(formList);
		return result;
	}
	
	@RequestMapping("findBySubjectRoomId")
	@ResponseBody
	public HTTPResult<YrFloorForm> findBySubjectRoomId(String subjectRoomId) throws Exception{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		YrSubjectRoom subjectRoom = subjectRoomService.findOne(subjectRoomId);
		if(null==subjectRoom){
			throwExp("找不到科室");
		}
		String[] floorsId = subjectRoom.getFloorsId().split(",");
		
		StringBuilder sb = new StringBuilder();
		for(int i=0;i<floorsId.length;i++){
			if(i==floorsId.length-1){
				sb.append('\''+floorsId[i]+"\'");
			}else{
				sb.append("\'"+floorsId[i]+"\'"+",");
			}
		}
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("floorName", "ASC");
		
		String sqlWhere = "floor_id in ("+sb.toString()+")";
		
		QueryResult<YrFloor> qr = yrFloorService.find(YrFloor.class, sqlWhere, new Object[]{},map);
		List<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		for(YrFloor floor : qr.getResultList()){
			formList.add(toForm(floor));
		}
		result.setData(formList);
		
		return result;
	}
	
	@RequestMapping("findByBuildingId")
	@ResponseBody
	public HTTPResult<YrFloorForm> findByBuildingId(String buildingId) throws Exception{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		
		LinkedHashMap<String, String> map = new LinkedHashMap<String, String>();
		map.put("floorName", "ASC");
		
		String sqlWhere = "building_id=?1";
		QueryResult<YrFloor> qr = yrFloorService.find(YrFloor.class, sqlWhere, new Object[]{buildingId},map);
		
		ArrayList<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		for(YrFloor floor : qr.getResultList()){
			formList.add(toForm(floor));
		}
		
		result.setData(formList);
		return result;
	}
	
	/**删除一条记录*/
	@RequestMapping("delete")
	@ResponseBody
	public HTTPResult<YrFloorForm> delete(String id) throws MyException{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		if(StringUtil.isEmpty(id)){
			throwExp("param.error");
		}
		yrFloorService.delete(id);
		return result;
	}
	
	/**获取一条记录*/
	@RequestMapping("findOne")
	@ResponseBody
	public HTTPResult<YrFloorForm> findOne(String id) throws MyException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		YrFloor entity = yrFloorService.findOne(id);
		List<YrFloorForm> formList = new ArrayList<YrFloorForm>();
		formList.add(ObjectUtils.objectToObject(entity, YrFloorForm.class));
		result.setData(formList);
		return result;
	}
	

	/**
	 * 批量删除
	 * @param data
	 * @return
	 * @throws SwallowException
	 * @throws MyException
	 */
	@RequestMapping("deleteBatch")
	@ResponseBody
	public HTTPResult<YrFloorForm> deleteBatch(String data) throws SwallowException, MyException{
		HTTPResult<YrFloorForm> result = new HTTPResult<YrFloorForm>();
		JsonMapperTool tool = new JsonMapperTool(data);
		String[] ids = tool.mapper("ids", String[].class);
		for(String id : ids){
			yrFloorService.delete(id);
		}
		return result;
	}
	
	@Override
	public YrFloorForm toForm(YrFloor e) throws Exception {
		YrFloorForm form = ObjectUtils.objectToObject(e, YrFloorForm.class);
		return form;
	}

	@Override
	public YrFloor toEntity(YrFloorForm f) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
